package com.example.demo.controller;

import com.example.demo.pojo.entity.Result;
import com.example.demo.pojo.entity.Task;
import com.example.demo.service.TasksService;
import com.example.demo.utils.BaseContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/tasks")
public class TasksController {
    @Autowired
    private TasksService tasksService;

    /**
     *逻辑删除任务
     */
    @DeleteMapping("/{id}")
    public Result deleteAllTasks(@PathVariable Long id) {
        tasksService.deleteAllTasks(id);
        return Result.success();
    }
    /**
     * 逻辑删除单个历史记录
     */
    @DeleteMapping("/GenerateHistory/{id}")
    public Result deleteGenerateHistory(@PathVariable Long id) {
        tasksService.deleteGenerateHistory(id);
        return Result.success();
    }

    /**
     * 任务详情
     */
    @GetMapping("/{id}")
    public Result getAllHistories_Tasks(@PathVariable Integer id) {
        return Result.success(tasksService.getAllHistories_Tasks(id));
    }

    @PostMapping("/create")
    public Result createTask(@RequestParam String title) {
        // 构造新任务实体
        Task task = new Task();
        // 获取当前用户id
        Long userId = BaseContext.getCurrentId();
        task.setUserId(userId);
        task.setTitle(title);
        // 设置创建时间、其他信息等（如需要）
        task.setCreateTime(java.time.LocalDateTime.now());
        // 设置默认值
        task.setIsDeleted(0);
        if(userId!=null) {
            // 调用服务层方法创建任务
            tasksService.createTask(task);
            return Result.success(task);
        }
        return Result.error("请先登录");
    }

    /**
     * 任务列表
     */
    @GetMapping()
    public Result getAllTasks(Integer page, Integer size ) {
        return Result.success(tasksService.getAllTasks(page, size));
    }

}
